﻿namespace Dng.TraficTracking
{
    using System;
    using System.IO;
    using System.Net;
    using System.Web;
    using System.Web.UI;
    using System.Xml;

    public class WeatherHandler : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            HttpContext context = this.Context;
            string content = string.Empty;
            if ((context.Request.QueryString["ID"] != null) && (context.Request.QueryString["ID"].Trim().Length >= 1))
            {
                HttpWebResponse response;
                string id = context.Request.QueryString["ID"];
                string url = "http://www.vnexpress.net";
                string weartherUrl = url + "/ListFile/Weather/" + id + ".xml";
                string imgUrl = url + "/Images/Weather/";
                HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(weartherUrl);
                try
                {
                    response = (HttpWebResponse) webRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    response = (HttpWebResponse) ex.Response;
                }
                StreamReader reader = new StreamReader(response.GetResponseStream());
                content = reader.ReadToEnd();
                reader.Close();
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(content);
                content = "";
                content = "<div id='img-Do'>";
                string altString = "DNG";
                string nodeName = "";
                for (int i = 0; i < 6; i++)
                {
                    nodeName = (i == 0) ? "AdImg" : ("AdImg" + i.ToString());
                    XmlNodeList nodes = doc.GetElementsByTagName(nodeName);
                    if (nodes[0].InnerText.Trim().Length > 0)
                    {
                        content = content + "<img alt='" + altString + "' class='img-weather' src='" + imgUrl + "/" + nodes[0].InnerText + "'>";
                    }
                }
                content = (content + "<img alt='" + altString + "' class='img-weather' src='" + imgUrl + "/c.gif'>") + "</div>";
            }
            context.Response.Write(content);
        }
    }
}

